import type {
  AddReq,
  DelReq,
  EditReq,
  InfoReq,
  UserPageRes,
} from '@fast-crud/fast-crud';

import type { AxiosRequestConfig } from '@vben/request';

import type { SopIsvInfoModel } from './model/sopIsvInfoModel';
import type { SopIsvKeysModel } from './model/sopIsvKeysModel';

import type { AudioVO, PageParams, PageResult, StatusUpdateVO } from '#/api';

import { requestClient } from '#/api/request';
import { ServicePrefixEnum } from '#/enums/commonEnum';
import { ContentTypeEnum } from '#/enums/httpEnum';

const MODULAR = 'sopIsvInfo';

const ServicePrefix = ServicePrefixEnum.SOPADMIN;

export const SopIsvInfoConfig = {
  Save: {
    url: `${ServicePrefix}/${MODULAR}`,
    method: 'POST',
  } as AxiosRequestConfig,
  Update: {
    url: `${ServicePrefix}/${MODULAR}`,
    method: 'PUT',
  } as AxiosRequestConfig,
  UpdateKeys: {
    url: `${ServicePrefix}/${MODULAR}/updateKeys`,
    method: 'POST',
  } as AxiosRequestConfig,
  ApplyFor: {
    url: `${ServicePrefix}/${MODULAR}/open/applyFor`,
    method: 'POST',
  } as AxiosRequestConfig,
  Submit: {
    url: `${ServicePrefix}/${MODULAR}/open/submit`,
    method: 'POST',
  } as AxiosRequestConfig,
  UpdateOpen: {
    url: `${ServicePrefix}/${MODULAR}/open/update`,
    method: 'PUT',
  } as AxiosRequestConfig,
};

export namespace SopIsvInfoApi {
  /**
   * 根据条件查询分页数据
   */
  export async function page(
    params: PageParams<SopIsvInfoModel.SopIsvInfoPageQuery>,
  ) {
    return requestClient.post<PageResult<SopIsvInfoModel.SopIsvInfoResultVO>>(
      `${ServicePrefix}/${MODULAR}/page`,
      params,
    );
  }
  /**
   * 根据id查询单条数据
   */
  export async function get(id: string) {
    return requestClient.get<SopIsvInfoModel.SopIsvInfoResultVO>(
      `${ServicePrefix}/${MODULAR}/${id}`,
    );
  }

  /**
   * 保存
   */
  export async function save(params: SopIsvInfoModel.SopIsvInfoSaveVO) {
    return requestClient.post<SopIsvInfoModel.SopIsvInfoResultVO>(
      SopIsvInfoConfig.Save.url as string,
      params,
    );
  }

  /**
   * 复制
   */
  export async function copy(id?: string) {
    return requestClient.post<SopIsvInfoModel.SopIsvInfoResultVO>(
      `${ServicePrefix}/${MODULAR}/copy`,
      { id },
      {
        headers: {
          'Content-Type': ContentTypeEnum.FORM_URLENCODED,
        },
      },
    );
  }

  /**
   * 修改
   */
  export async function update(params: SopIsvInfoModel.SopIsvInfoUpdateVO) {
    return requestClient.put<SopIsvInfoModel.SopIsvInfoResultVO>(
      SopIsvInfoConfig.Update.url as string,
      params,
    );
  }

  export async function updateOpen(params: SopIsvInfoModel.SopIsvInfoUpdateVO) {
    return requestClient.put<string>(
      SopIsvInfoConfig.UpdateOpen.url as string,
      params,
    );
  }

  /**
   * 修改状态
   */
  export async function updateStatus(params: StatusUpdateVO) {
    return requestClient.put<boolean>(
      `${ServicePrefix}/${MODULAR}/updateStatus`,
      params,
    );
  }

  /** 审核 */
  export async function examine(params: AudioVO) {
    return requestClient.post<string>(
      `${ServicePrefix}/${MODULAR}/examine`,
      params,
    );
  }
  /**
   * 删除
   */
  export async function remove(data: string[]) {
    return requestClient.delete<boolean>(`${ServicePrefix}/${MODULAR}`, {
      data,
    });
  }

  /**
   * 根据id查询单条数据
   */
  export async function getKeys(isvId: string) {
    return requestClient.get<SopIsvKeysModel.SopIsvKeysResultVO>(
      `${ServicePrefix}/${MODULAR}/getKeys?isvId=${isvId}`,
    );
  }
  /** 生成 秘钥key */
  export async function createKeys(params: SopIsvKeysModel.IsvKeysGenVO) {
    return requestClient.post<SopIsvKeysModel.KeyStore>(
      `${ServicePrefix}/${MODULAR}/createKeys`,
      params,
    );
  }
  /** 修改秘钥 */
  export async function updateKeys(params: SopIsvKeysModel.SopIsvKeysUpdateVO) {
    return requestClient.post<boolean>(
      SopIsvInfoConfig.UpdateKeys.url as string,
      params,
    );
  }

  /**
   * 查看自己的isv
   * @param params
   * @returns
   */
  export async function openPage(
    params: PageParams<SopIsvInfoModel.SopIsvInfoPageQuery>,
  ) {
    return requestClient.post<PageResult<SopIsvInfoModel.SopIsvInfoResultVO>>(
      `${ServicePrefix}/${MODULAR}/open/page`,
      params,
    );
  }
  /**
   * 暂存申请
   */
  export async function applyFor(params: SopIsvInfoModel.SopIsvInfoApplyForVO) {
    return requestClient.post<string>(
      SopIsvInfoConfig.ApplyFor.url as string,
      params,
    );
  }

  /**
   * 提交申请
   */
  export async function submit(params: SopIsvInfoModel.SopIsvInfoSubmitVO) {
    return requestClient.post<string>(
      SopIsvInfoConfig.Submit.url as string,
      params,
    );
  }

  // fast-crud 需要的接口
  export const pageRequest = async (
    pageQuery: PageParams<SopIsvInfoModel.SopIsvInfoPageQuery>,
  ): Promise<UserPageRes> => {
    return await page(pageQuery);
  };

  export const openPageRequest = async (
    pageQuery: PageParams<SopIsvInfoModel.SopIsvInfoPageQuery>,
  ): Promise<UserPageRes> => {
    return await openPage(pageQuery);
  };

  export const infoRequest = async (ctx: InfoReq): Promise<UserPageRes> => {
    const { row } = ctx;
    // 请求后台查询数据
    return row;
  };

  export const applyForRequest = async (req: AddReq) => {
    const { form } = req;
    return await applyFor(form);
  };

  export const submitRequest = async (req: AddReq) => {
    const { form } = req;
    return await submit(form);
  };

  export const addRequest = async (req: AddReq) => {
    const { form } = req;
    return await save(form);
  };

  export const editRequest = async (ctx: EditReq) => {
    const { form } = ctx;
    return await update(form);
  };

  export const editOpenRequest = async (ctx: EditReq) => {
    const { form } = ctx;
    return await updateOpen(form);
  };

  export const delRequest = async (ctx: DelReq) => {
    const { row } = ctx;
    return await remove([row.id]);
  };

  export const updateKeysRequest = async (req: AddReq) => {
    const { form } = req;
    return await updateKeys(form);
  };
  export const examineRequest = async (req: AddReq) => {
    const { form } = req;
    return await examine(form);
  };
}
